/*
 * Design by  free.wang <freefis@gmail.com>
 * Licence unser BSD  lisence
 * Complete on May 30 21:30 , 2009 
 *
 * Environment:
 *     GCC 4.3.2
 *     Ubuntu / Linux i386
 *     Mac OS X / FreeBSD 4.x
 */
#include "basic.h"

/*
 * 变异 variation()
 * 交叉 cross ()
 */



plt * init_plt(){
	return (plt *)malloc(sizeof(plt));
}

void pltshow(plt * population)
{
    int i,j;
	printf("Result is:%d",(int)(population->result));
    printf("{\n");         	
    for(j=0;j<3;j++){
        for(i=0;i<6;i++)
            printf("\t%d",(int)((population->parement)[j][i]) );
        printf("\n");
    }
    printf("}\n");
}

void natural_selection(plt * Top5Population[5],plt * ExamplePopulation[100])
{
    /* 
     * Natural Selection from Darwin's <<THe Theory of Evolution>> 
     * In our Code,we get TOP 5 of the Population.
     */
	int i,j;
    for(j=0;j<5;j++)
    {   
	    Top5Population[j] = init_plt();
		memcpy(Top5Population[j],ExamplePopulation[0],sizeof(plt));
        for(i=0;i<100;i++)
		{                  
			if( (ExamplePopulation[i]->result) < (Top5Population[j]->result) )
            {
                if(
				   	  (j==0)		|
				   	  (  (j>=1)  & ( (ExamplePopulation[i]->result) > (Top5Population[j-1]->result) ) )
				  )
					memcpy(Top5Population[j],ExamplePopulation[i],sizeof(plt));
            }                            
        } 
    }                                      
	  
}




/* random seed 个体*/
void generate_seed(double seed[3][6])
{
    int i;
    for(i=0;i<6;i++){
        seed[0][i] = randint(1000,1400); /* products */
        seed[1][i] = randint(10,14);     /* workers  */
        seed[2][i] = randint(0,2);       /* extras   */
    }
}

          
                            

        

void generate_example_population(plt * ExamplePopulation[100]){ 
	
	/* initial seed for population */
	double seedset[100][3][6]; 
	
	/*
		loop in 100 times
		In this Loop, Random Generate 100 Populations which follow the Rule. 
	*/
	int i=0;
	do{ 
		generate_seed(seedset[i]);	
		ExamplePopulation[i] = init_plt();
		
		memcpy(ExamplePopulation[i]->parement,seedset[i],sizeof(seedset[i]));
		ExamplePopulation[i]->result = all_cost( seedset[i] );
		
		i++;
	} while(i<100);
	
   
}
